package com.xjscrm.console.chat.frame.send;

import com.xjscrm.console.chat.enums.ChatMsgType;
import com.xjscrm.console.chat.frame.recv.RBaseMsg;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import org.slf4j.MDC;

import java.util.Objects;

/**
 * @author mingyang.chen create in 2024/3/12 15:57
 */
@Data
@ApiModel
public class SResultMsg {

    @ApiModelProperty("请求时的id, 响应时会对应上")
    private Long reqId;

    @ApiModelProperty("运营商id, 前端校验一下")
    private Long merId;

    @ApiModelProperty("消息出后处理成功")
    private Boolean success;

    @ApiModelProperty("消息")
    private String msg;

    @ApiModelProperty(value = "结果类型")
    private Integer resultType;

    @ApiModelProperty(value = "结果类型描述")
    private String resultTypeDesc;

    @ApiModelProperty(value = "机器人id")
    private Long deviceUniqueId;

    @ApiModelProperty(value = "员工id")
    private Long staffId;

    private String traceId = MDC.get("traceId");

    public void fill(RBaseMsg reqId) {
        if (Objects.nonNull(reqId)) {
            this.setReqId(reqId.getReqId());
            this.setMerId(reqId.getMerId());
        }
    }

    public static SResultMsg fail(RBaseMsg reqId, String content) {
        SResultMsg wrapperMsg = new SResultMsg();
        if (Objects.nonNull(reqId)) {
            wrapperMsg.setReqId(reqId.getReqId());
            wrapperMsg.setMerId(reqId.getMerId());
        }
        wrapperMsg.setSuccess(false);
        wrapperMsg.setResultType(ChatMsgType.s_error_msg.getCode());
        wrapperMsg.setResultTypeDesc(ChatMsgType.s_error_msg.getDesc());
        wrapperMsg.setMsg(content);
        return wrapperMsg;
    }

    public static SResultMsg fail(RBaseMsg reqId, String content, ChatMsgType msgType) {
        SResultMsg wrapperMsg = new SResultMsg();
        if (Objects.nonNull(reqId)) {
            wrapperMsg.setReqId(reqId.getReqId());
            wrapperMsg.setMerId(reqId.getMerId());
        }
        wrapperMsg.setSuccess(false);
        wrapperMsg.setResultType(msgType.getCode());
        wrapperMsg.setResultTypeDesc(msgType.getDesc());
        wrapperMsg.setMsg(content);
        return wrapperMsg;
    }


    public static SResultMsg success(RBaseMsg baseMsg, String content) {
        SResultMsg wrapperMsg = new SResultMsg();
        wrapperMsg.setReqId(baseMsg.getReqId());
        wrapperMsg.setMerId(baseMsg.getMerId());
        wrapperMsg.setSuccess(true);
        wrapperMsg.setResultType(ChatMsgType.s_success_msg.getCode());
        wrapperMsg.setMsg(content);
        return wrapperMsg;
    }

    public static SResultMsg success(RBaseMsg baseMsg, String content, ChatMsgType msgType) {
        SResultMsg wrapperMsg = new SResultMsg();
        wrapperMsg.setReqId(baseMsg.getReqId());
        wrapperMsg.setMerId(baseMsg.getMerId());
        wrapperMsg.setSuccess(true);
        wrapperMsg.setResultType(msgType.getCode());
        wrapperMsg.setMsg(content);
        return wrapperMsg;
    }
}
